package tt.dz.egobus.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.common.CommonApiService;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.busservice.BusServiceApiService;
import tt.dz.egobus.api.managerservice.busservice.BusServiceVo;
import tt.dz.egobus.api.util.ResponseHelper;
import tt.dz.egobus.servicemanager.entities.BusService;
import tt.dz.egobus.servicemanager.service.BusServiceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.api.util.ResponseHelper.copy;
import static tt.dz.egobus.bean.I18NCodeEnum.RPC_PARAM_ILLEGAL_ERROR;

/**
 * @author Anson
 * @date 17-3-15
 * @name 车辆类型服务实现类
 * @since 1.0.1
 */
@Service(version = "1.0.1", interfaceClass = BusServiceApiService.class)
public class BusServiceApiServiceImpl extends CommonApiService implements BusServiceApiService {


    public static final String BUS_SERVICE_SERVICE_NAME = "busServiceServiceImpl";


    @Override
    public ResponseBack<BusServiceVo> queryInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusServiceService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusServiceService.class);
        BusService entity = service.get(id);
        if (entity == null) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusServiceVo response = copy(entity, new BusServiceVo());
        return ResponseBack.success(response);
    }

    @Override
    public ResponseBack<List<BusServiceVo>> queryInfos(String[] ids) throws ResponseException {

        if (null == ids || ids.length == 0) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusServiceService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusServiceService.class);
        List<BusService> entities = service.queryByIds(ids);
        List<BusServiceVo> response = ResponseHelper.copy(entities, BusServiceVo.class);
        return ResponseBack.success(response);
    }

    @Override
    public ResponseBack<List<BusServiceVo>> queryValidate() throws ResponseException {
        Map<String, Object> params = new HashMap<String, Object>();
        String op = "eq";
        params.put(op + "#isActive", true);
        BusServiceService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusServiceService.class);
        List<BusService> entities = service.list(params);
        List<BusServiceVo> response = ResponseHelper.copy(entities, BusServiceVo.class);
        return ResponseBack.success(response);
    }

    @Override
    public ResponseBack<BusServiceVo> queryInfo(int businessCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        String op = "eq";
        params.put(op + "#isActive", true);
        params.put(op + "#businessCode", businessCode);
        BusServiceService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusServiceService.class);
        List<BusService> entities = service.list(params);
        BusServiceVo vo = new BusServiceVo();
        if (entities.isEmpty()) {
            return ResponseBack.success(vo);
        } else {
            ResponseHelper.copy(entities.get(0), vo);
        }
        return ResponseBack.success(vo);
    }

}
